package com.example.springboot.controller.requstParams;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/15 23:03
 */
@Controller
@RequestMapping("/model")
public class MapAndModelParamController {
    /**
     * 对map或model里面的操作都可以给request域中放数据
     *
     * @param map      map类型的参数会使用MapMethodProcessor参数解析器，
     *                 会返回BindingAwareModelMap这个是model也是map
     * @param model    model类型会用ModelMethodProcessor的参数解析器
     *                 此时也会调用mavContainer.getModel()方法，此时也会返回BindingAwareModelMap
     *                 此时会和map类型使用一个对象
     * @param request
     * @param response
     * @return 目标方法执行完成，会把所有数据放在modelAndViewContainer中
     */
    @GetMapping("/param")
    public String testParam(Map<String, Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response) {
        map.put("hello", "world6656");
        model.addAttribute("world", "hello666");
        request.setAttribute("message", "czk");
        Cookie cookie = new Cookie("c1", "v1");
        cookie.setDomain("localhost");
        response.addCookie(cookie);
        return "forward:/success";
    }
}
